// 登录用户的仓库数据
import { login, logout, whoAmI } from '../services/userService'
export default {
  namespaced: true, // 开启命名空间
  state: {
    data: null, // 当前登录的用户信息
    isLoading: false // 是否正在远程加载中
  },
  mutations: {
    setIsLoading (state, payload) {
      state.isLoading = payload
    },
    setData (state, payload) {
      state.data = payload
    }
  },
  actions: {
    async login (context, payload) {
      context.commit('setIsLoading', true)
      var res = await login(payload) // {code:0, msg:'', data:xxx}
      var flag = false
      if (res.code === 0) {
        // 登录成功
        context.commit('setData', res.data)
        flag = true
      }
      context.commit('setIsLoading', false)
      return flag
    },
    async whoAmI (context) {
      context.commit('setIsLoading', true)
      var res = await whoAmI()
      context.commit('setData', res)
      context.commit('setIsLoading', false)
    },
    logout (context) {
      logout()
      context.commit('setData', null)
    }
  }
}
